<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%3;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "დავალებები სტატუსების მიხედვით";
$text[1] 	= "'$type'- კატეგორიების მიხედვით";
$text[2] 	= "'$type'-'$category'-ქვე-კატეგორიების მიხედვით";

if ($type=="გადაცემულია გასარკვევად")  $c=0;
elseif ($type=="გარკვევის პროცესშია") $c=1;
elseif ($type=="მოგვარებულია") $c=2;
elseif ($type=="დასრულებული") $c=3;

//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("SELECT 	cat.`name` as c_name,
											COUNT(*),
											CONCAT(ROUND(
											COUNT(*)/(SELECT COUNT(*)
												FROM 	task
												LEFT JOIN 	`category` AS cat ON task.call_type_id=cat.id
                                                LEFT JOIN 	`category` AS sub_cat ON task.subcategory_id=sub_cat.id
                                                WHERE (task.`status`=$c)  and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' 
												AND cat.`name` !=''
												)*100,2),'%')
									
									FROM 	task
									LEFT JOIN 	`category` AS cat ON task.call_type_id=cat.id
                                    LEFT JOIN 	`category` AS sub_cat ON task.subcategory_id=sub_cat.id
									WHERE (task.`status`=$c)  and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end'
									AND cat.`name` !=''
									GROUP BY c_name");
		$text[0]=$text[1];
	break;
	case 2:
			$result = mysql_query("SELECT 	sub_cat.`name` as c_name,
											COUNT(*),
											CONCAT(ROUND(
											COUNT(*)/(SELECT COUNT(*)
												FROM 	task
												LEFT JOIN 	`category` AS cat ON task.call_type_id=cat.id
                                                LEFT JOIN 	`category` AS sub_cat ON task.subcategory_id=sub_cat.id
                                                WHERE (task.`status`=$c)  and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' AND cat.`name` = '$category'
												AND cat.`name` !=''
												)*100,2),'%')
									
									FROM 	task
									LEFT JOIN 	`category` AS cat ON task.call_type_id=cat.id
                                    LEFT JOIN 	`category` AS sub_cat ON task.subcategory_id=sub_cat.id
									WHERE (task.`status`=$c)  and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' AND cat.`name` = '$category'
									AND cat.`name` !=''
									GROUP BY c_name");
		$text[0]=$text[2];
	break;
	default:
		$result = mysql_query("SELECT `status`.`call_status` as type,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM task
										WHERE DATE(task.date) >= '$start' AND DATE(task.date) <= '$end')*100,2),'%')
								FROM 	task
								JOIN  `status` ON task.`status`=`status`.id
								WHERE DATE(task.date) >= '$start' AND DATE(task.date) <= '$end'
								GROUP BY 	type;");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
	case 'get_in_page':
		mysql_query("SET @i = 0;");
	  	$rResult = mysql_query("SELECT	`task`.id,
                        				`task`.id,
                        				task.`date`,
                        				task.`phone` AS `phone`,
                        				c_type.`name`  AS `call_type`,
                        				cat.`name`  AS `category`,
                        				sub_cat.`name`  AS `sub_category`,
                        				object.`name`  AS object,
                        				persons.`name`,
                        				`status`.`call_status`
                                FROM 	`task`
                                LEFT JOIN 	`incomming_call` ON task.incomming_call_id=incomming_call.id
                                LEFT JOIN 	`category` AS c_type ON task.call_type_id=c_type.id
                                LEFT JOIN 	`category` AS cat ON task.category_id=cat.id
                                LEFT JOIN 	`category` AS sub_cat ON task.subcategory_id=sub_cat.id
                                LEFT JOIN 	`object` ON task.object_id=object.id
                                LEFT JOIN 	`status` ON	task.`status`=`status`.`id`
                                JOIN        `users` ON task.responsible_user_id = users.id
                                JOIN        `persons` ON users.person_id = persons.id
                                WHERE 		DATE(task.date) >= '$start' AND DATE(task.date) <= '$end' AND `task`.`actived`=1 AND sub_cat.`name` ='$_REQUEST[rid]' AND task.`status`=$c");
	  
$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[] = $aRow[$i];
				$a=$aRow;
				
				{
				
				}
			}
			$data['aaData'][] = $row;
		}
		echo json_encode($data); return 0;
		break;
	default :
		echo "Action Is Null!";
		break;

}



?>